//
//  SWPSUtil.cpp
//  swp
//
//  Created by Rune Laugesen on 31/01/12.
//  Copyright 2012 Copenhagen University. All rights reserved.
//

#include <iostream>
#include "SWPSUtil.h"

using namespace std;

SWPS SWPSUtil::ReadSWPS(string filename)
{
    SWPS swps;
    swps.featurecount = 0;
    swps.tokencount = 0;
    vector<Feature> features;
    char line[LINEBUFFER] = "";
    char* tokens;
    
    ifstream file(filename.c_str());
    if (file.is_open())
    {
        while (!file.eof())
        {
            Feature feature;
            file.getline(line, LINEBUFFER);
            feature.offset = atoi(strtok(line, "INDEX="));
            feature.index = atoi(strtok(NULL, "FEATURE="));
            feature.weight = atof(strtok(NULL, "WEIGHT="));
            features.push_back(feature);
            tokens = strtok(NULL, "\n");
            swps.featurecount++;
            swps.tokencount = (swps.tokencount < abs(feature.index)) ? abs(feature.index) : swps.tokencount;
        }
        
        swps.features = features;
    }
    return swps;
}

vector< vector<string> > SWPSUtil::ReadSWPSData(string filename, SWPS swps)
{
    vector< vector<string> > data(6);
    char line[LINEBUFFER];
    char* tokens;
    int linecount = 0;
    
    ifstream file(filename.c_str());
    if (file.is_open())
    {
        while (!file.eof()) 
        {
            file.getline(line, LINEBUFFER);
            if (line[0] == '\0')
            {
                for (int i = 0; i < 6; i++)
                {
                    data[i].push_back("");
                }
            }
            else
            {
                tokens = strtok(line, "|");
                data[0].push_back(SWPSUtil::StringToLower(tokens));
                for (int i = 1; i < 6; i++)
                {
                    tokens = strtok(NULL, "|");
                    data[i].push_back(tokens);
                }
            }
            linecount++;
        }
    }
    return data;
}

map<string, vector<string> > SWPSUtil::ReadSWPSFeatureData(string filename, SWPS swps)
{
    map<string, vector<string> > data;
    char line[LINEBUFFER];
    char* tokens;
    int linecount = 0;
    
    ifstream file(filename.c_str());
    if (file.is_open())
    {
        while (!file.eof())
        {
            file.getline(line, LINEBUFFER);
            if (swps.featurecount > 0)
            {
                tokens = strtok(line, "|");
                string word = SWPSUtil::StringToLower(tokens);
                vector<string> features(6);
                features[0] = word;
                for (int i = 1; i < 6; i++)
                {
                    tokens = strtok(NULL, "|");
                    features[i] = tokens;
                }
                data.insert(pair<string, vector<string> >(word, features));
            }
            linecount++;
        }
    }
    return data;
}

vector< vector<string> > SWPSUtil::ReadSwitchBoardData(string filename, SWPS swps)
{
    vector< vector<string> > data(swps.featurecount);
    char line[LINEBUFFER];
    char* tokens;
    int linecount = 0;
    
    ifstream file(filename.c_str());
    if (file.is_open())
    {
        while (!file.eof()) 
        {
            file.getline(line, LINEBUFFER);
            if (line[0] == '\0')
            {
                for (int i = 0; i < swps.featurecount; i++) 
                {
                    data[i].push_back("");
                }
            }
            else
            {
                tokens = strtok(line, " ");
                data[0].push_back(SWPSUtil::StringToLower(tokens));
                for (int i = 1; i < swps.featurecount; i++)
                {
                    if (swps.tokencount == i)
                    {
                        tokens = strtok(NULL, " ");
                        data[i].push_back(tokens);
                    }
                }
            }
            linecount++;
        }
    }
    return data;
}

vector< vector<string> > SWPSUtil::ReadCONLLData(string filename)
{
    return CONLLConverter::ReadToSWPS(filename);
}

vector< vector<string> > SWPSUtil::buildFeatures(string filename, SWPS swps)
{
    vector< vector<string> > data(swps.featurecount);
    
    return data;
}

string SWPSUtil::StringToLower(string strToConvert)
{
    for(unsigned int i=0;i<strToConvert.length();i++)
    {
        strToConvert[i] = tolower(strToConvert[i]);
    }
    return strToConvert;//return the converted string
};